package com.qeegoo.b2bautozityp.moudle.home.view;


import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.constant.Constants;
import com.qeegoo.b2bautozityp.databinding.ActivitySplashBinding;
import com.qeegoo.b2bautozityp.moudle.base.BaseActivity;
import com.qeegoo.b2bautozityp.moudle.detail.bean.DataSource;
import com.qeegoo.b2bautozityp.moudle.detail.bean.NetHelper;
import com.qeegoo.b2bautozityp.moudle.home.model.CheckUpdateBean;

import base.lib.util.NavigateUtils;
import base.lib.util.PreferencesUtils;
import base.lib.util.RxHelper;
import base.lib.util.ToastUtils;
import rx.Subscriber;

/**
 * 王少岩 在 2017/3/10 创建了它
 */

public class SplashActivity extends BaseActivity<ActivitySplashBinding> implements DataSource.CallBack<CheckUpdateBean> {
    private boolean mIsSkip = false;



    private CheckUpdateBean checkUpdateBean;
    private ProgressBar pb_download;
    private DownloadBroadcastReceiver receiver;
    private long startTimeMillis;
    private long extraTimeMillis;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initInjector() {


    }

    @Override
    protected void initViews() {


        startTimeMillis = System.currentTimeMillis();

        NetHelper.mobileCheckupdate(this);  //检查是否有新版本
    }

    private void goToNext(long extraTimeMillis) {
        RxHelper.countdown((int) (extraTimeMillis/1000))
                .compose(this.bindToLife())
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        doSkip();
                    }

                    @Override
                    public void onError(Throwable e) {
                        doSkip();
                    }

                    @Override
                    public void onNext(Integer integer) {
                    }
                });
    }

    private void doSkip() {
        if (!mIsSkip) {
            mIsSkip = true;
            if (PreferencesUtils.getBoolean(Constants.sUser_loginFlag, false)) {
                NavigateUtils.startActivity(MainActivity.class);
            } else {
                NavigateUtils.startActivity(MainActivity.class);
            }
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        // 不响应后退键
        return;
    }


    @Override
    public void onDataLoaded(CheckUpdateBean checkUpdateBean) {

        long currentTimeMillis = System.currentTimeMillis();
        extraTimeMillis = currentTimeMillis - startTimeMillis;
        this.checkUpdateBean = checkUpdateBean;
        int is_update = checkUpdateBean.getStatus().getIs_update();
        if (is_update == Constants.IsCurrentVersion) {

            goToNext(extraTimeMillis);

        } else if (is_update == Constants.NeedUpdate) {
            showDownLoadDialog(Constants.NeedUpdate);
        } else if (is_update == Constants.ForceUpdate) {
            showDownLoadDialog(Constants.ForceUpdate);
        }



    }

    private void showDownLoadDialog(int isForceUpdate) {



        //注册广播接收器
        receiver = new DownloadBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(DownLoadService.ACTION_UPDATE);
        registerReceiver(receiver, filter);

        AlertDialog.Builder builder = new AlertDialog.Builder(SplashActivity.this);
        View dialog_view = View.inflate(this, R.layout.dialog_download, null);
        pb_download = (ProgressBar) dialog_view.findViewById(R.id.pb_download);
        Button btn_cancel = (Button) dialog_view.findViewById(R.id.btn_cancel);
        if(Constants.NeedUpdate==isForceUpdate) {
            btn_cancel.setVisibility(View.VISIBLE);
        }else {
            btn_cancel.setVisibility(View.GONE);
        }
        Button btn_start = (Button) dialog_view.findViewById(R.id.btn_start);
        builder.setView(dialog_view);
        AlertDialog dialog = builder.create();
        btn_start.setEnabled(true);

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goToNext(extraTimeMillis);
                dialog.dismiss();
              DownLoadService.isCancel=true;
            }
        });
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SplashActivity.this, DownLoadService.class);
                intent.putExtra("path", checkUpdateBean.getStatus().getUpdateUrl());
                startService(intent);
                btn_start.setEnabled(false);
            }
        });


        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    public void onDataNotAvaiable(String str) {

        ToastUtils.showToast(str);
    }

    class DownloadBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            int progress = intent.getIntExtra(DownLoadService.DOWNLOAD_PROGRESS, 0);
            int fileLength = (int) intent.getLongExtra("fileLength", 0);
            pb_download.setMax(fileLength);
            pb_download.setProgress(progress);


        }
    }


    @Override
    protected void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }
}
